package libaryinterface.admin;

import pojo.Category;
import pojo.User;
import service.BookRecordService;
import service.CategoryService;
import service.UserService;
import service.impl.BookRecordServiceImpl;
import libaryinterface.interfaceutils.MyDialogDemo;
import service.impl.CategoryServiceImpl;
import service.impl.UserServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//管理员添加借阅记录
public class AdminAddRecordInterface extends JFrame{
    JFrame f1;
    JPanel p1;
    JButton b1;
    JLabel l1,l2,l3,l4,l5,l6;
    JTextField t1,t2,t3,t4,t5,t6;
    BookRecordService bookRecordService = new BookRecordServiceImpl();
    UserService userService = new UserServiceImpl();
    CategoryService categoryService = new CategoryServiceImpl();
    public AdminAddRecordInterface()
    {
        f1 = new JFrame("添加信息");
        //1.把图片添加到标签里（把标签的大小设为和图片大小相同），把标签放在分层面板的最底层；
        ImageIcon bg = new ImageIcon("src//picture//asdf.jpg");
        JLabel label = new JLabel(bg);
        label.setSize(bg.getIconWidth(),bg.getIconHeight());
        f1.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //2.把窗口面板设为内容面板并设为透明、流动布局。
        JPanel p1=(JPanel)f1.getContentPane();
        p1.setOpaque(false);
        p1.setLayout((new FlowLayout()));

        Font f = new Font("楷体",Font.PLAIN,20);
        b1 = new JButton("添加");

        l2 = new JLabel("书名：");
        l3 = new JLabel("借阅人：");
        //l4 = new JLabel("借阅时长：");
        l5 = new JLabel("借阅数量：");

        l2.setFont(f);
        l2.setForeground(Color.white);

        l3.setFont(f);
        l3.setForeground(Color.white);

        //l4.setFont(f);
        //l4.setForeground(Color.white);

        l5.setFont(f);
        l5.setForeground(Color.white);

//        l2.setFont(new Font("黑体", Font.PLAIN, 36));
        t2 = new JTextField(18);
        t3 = new JTextField(18);
       // t4 = new JTextField(18);
        t5 = new JTextField(18);

        p1.setLayout(null);
        b1.setBounds(250,450,80,30);

        l2.setBounds(160,140,100,30);
        l3.setBounds(160,200,100,30);
       // l4.setBounds(160,260,100,30);
        l5.setBounds(160,320,100,30);


        t2.setBounds(270,140,200,30);
        t3.setBounds(270,200,200,30);
       // t4.setBounds(270,260,200,30);
        t5.setBounds(270,320,200,30);


         p1.add(l2); p1.add(l3);// p1.add(l4);
         p1.add(l5);
         p1.add(t2); p1.add(t3); //p1.add(t4);
         p1.add(t5);
        p1.add(b1);

        f1.setVisible(true);
        f1.setBounds(600,250,600,700);

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(t2.getText().equals("")||t3.getText().equals("")||t5.getText().equals(""))
                {
                    new MyDialogDemo("书籍信息不全！");
                    return;
                }
                String bookName = t2.getText();
                String userName = t3.getText();
                //Long loanDay = Long.valueOf(t4.getText());
                Long loanNumber = Long.valueOf(t5.getText());
                //根据借阅人姓名获得id
                User user = userService.getUserByName(userName);
                //根据借阅人id 获得其对应的分类
                Category category = categoryService.getCategoryByAccount(user.getAccount());
                //根据分类获得最大借阅天数
                Long maxLoanNumber = category.getLoanNumber();
                //添加一条借阅记录
                boolean flag = bookRecordService.addBookRecord(bookName,userName, maxLoanNumber, loanNumber);
                if( flag){
                    new MyDialogDemo("添加成功!");
                }
            }
        });


    }
}